#include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"

using namespace std;

int testing() {
//int main(int argc, char* args[]) {

	// Initialize the SDL
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		cerr << "SDL_Init() Failed: " << SDL_GetError() << endl;
		exit(1);
	}

	// Set the video mode
	SDL_Surface* display;
	display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
	if (display == NULL) {
		cerr << "SDL_SetVideoMode() Failed: " << SDL_GetError() << endl;
		exit(1);
	}

	// Set the title bar
	SDL_WM_SetCaption("SDL Tutorial", "SDL Tutorial");

	// Initialize SDL_ttf library
	if (TTF_Init() != 0) {
		cerr << "TTF_Init() Failed: " << TTF_GetError() << endl;
		SDL_Quit();
		exit(1);
	}

	// Load a font
	TTF_Font *font;
	font = TTF_OpenFont("FreeSans.ttf", 24);
	if (font == NULL) {
		cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
		TTF_Quit();
		SDL_Quit();
		exit(1);
	}

	// Write text to surface
	SDL_Surface *text;
	SDL_Color text_color = { 255, 255, 255 };
	text = TTF_RenderText_Solid(font,
			"A journey of a thousand miles begins with a single step.", text_color);

	if (text == NULL) {
		cerr << "TTF_RenderText_Solid() Failed: " << TTF_GetError() << endl;
		TTF_Quit();
		SDL_Quit();
		exit(1);
	}

	// Load the image
	SDL_Surface* image;
	image = SDL_LoadBMP("gptutorials.bmp");

	if (image == NULL) {
		cerr << "SDL_LoadBMP() Failed: " << SDL_GetError() << endl;
		exit(1);
	}

	/* Apply the image to the display
	if (SDL_BlitSurface(image, NULL, display, NULL) != 0) {
		cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
		exit(1);
	}*/


	// Main loop
	SDL_Event event;

	// Image motion variables
	float x = 0.1f, y = 0.1f;
	float xVel = 50.0f, yVel = 40.0f;

	// Timing variables
	Uint32 old_time, current_time;
	float ftime;

	// Need to initialize this here for event loop to work
	current_time = SDL_GetTicks();

	while (1) {
		// Update the timing information
		old_time = current_time;
		current_time = SDL_GetTicks();
		ftime = (current_time - old_time) / 1000.0f;

		// Check for messages
		if (SDL_PollEvent(&event)) {
			// Check for the quit message
			if (event.type == SDL_QUIT) {
				// Quit the program
				break;
			}
		}

		// Update the image position
		x += (xVel * ftime);
		y += (yVel * ftime);

		// Check boundaries
		if (x <= 0.0f)
			xVel *= -1.0f;

		if (x >= (display->w - image->w))
			xVel *= -1.0f;

		if (y <= 0.0f)
			yVel *= -1.0f;

		if (y >= (display->h - image->h))
			yVel *= -1.0f;

		// Clear the screen
		if (SDL_FillRect(display, NULL, SDL_MapRGB(display->format, 0, 0, 0))
				!= 0) {
			cerr << "SDL_FillRect() Failed: " << SDL_GetError() << endl;
			break;
		}

		SDL_Rect dest;
		dest.x = static_cast<int>(x);
		dest.y = static_cast<int>(y);

		// Apply the logo to the display
		if (SDL_BlitSurface(image, NULL, display, &dest) != 0) {
			cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
			break;
		}

		// Apply the text to the display
			if (SDL_BlitSurface(text, NULL, display, NULL) != 0) {
				cerr << "SDL_BlitSurface() Failed: " << SDL_GetError() << endl;
				break;
			}
		//Update the display
		SDL_Flip(display);
	}
	// Game loop will go here...

	// Tell the SDL to clean up and shut down
	SDL_Quit();

	return 0;
}
